package chat;

import java.io.*;
import java.util.*;

public class ActivePeerManager
    implements ActivePeer.ActivePeersSupport , PendingPeerManager.ActivePeersSupport
{
    private MyInfo zMyInfo;
    private UserDialog zUserDialog;
    private PendingPeerManager zPendingPeerManager;
    private List zListofPeers = new ArrayList();
    private int zPeerCnt = 0;

    public ActivePeerManager( MyInfo pMyInfo , UserDialog pUserDialog , PendingPeerManager pPendingPeerManager )
    {
        IllegalArgument.ifNull( "MyInfo" , zMyInfo = pMyInfo );
        IllegalArgument.ifNull( "UserDialog" , zUserDialog = pUserDialog );
        IllegalArgument.ifNull( "PendingPeerManager" , zPendingPeerManager = pPendingPeerManager );
        zUserDialog.setActivePeerManager( this );
        zPendingPeerManager.start( this );
    }

    public synchronized void removeActivePeer( ActivePeer pRequester )
    {
        IllegalArgument.ifNull( "Requester" , pRequester );
        zListofPeers.remove( pRequester );
    }

    public synchronized PeerInfo[] getPeerInfos()
    {
        PeerInfo[] retval = new PeerInfo[ zListofPeers.size() ];
        for ( int i = 0 ; i < retval.length ; i++ )
            retval[ i ] = ((ActivePeer) (zListofPeers.get( i ))).getPeerInfo();
        return retval;
    }

    public synchronized void sendToAllCHAT( String pMessage )
    {
        IllegalArgument.ifNull( "Message" , pMessage );
        for ( Iterator it = zListofPeers.iterator() ; it.hasNext() ; )
            ((ActivePeer) it.next()).sendCHAT( pMessage );
    }

    public synchronized void sendToAllNAME()
    {
        for ( Iterator it = zListofPeers.iterator() ; it.hasNext() ; )
            ((ActivePeer) it.next()).sendNAME();
    }

    public synchronized ActivePeer getPeerListenerByID( String pID )
    {
        if ( pID != null )
            for ( Iterator it = zListofPeers.iterator() ; it.hasNext() ; )
            {
                ActivePeer peer = (ActivePeer) it.next();
                if ( pID.equals( peer.getPeerInfo().getID() ) )
                    return peer;
            }
        return null;
    }

    public synchronized boolean isAlreadyConnected( PeerInfo pPeerInfo )
    {
        if ( pPeerInfo != null )
            for ( Iterator it = zListofPeers.iterator() ; it.hasNext() ; )
                if ( ((ActivePeer) it.next()).getPeerInfo().equals( pPeerInfo ) )
                    return true;

        return false;
    }

    public synchronized void addActivePeer( PeerInfo pPeerInfo , InputStream pInputStream , OutputStream pOutputStream )
    {
        IllegalArgument.ifNull( "PeerInfo" , pPeerInfo );
        IllegalArgument.ifNull( "InputStream" , pInputStream );
        IllegalArgument.ifNull( "OutputStream" , pOutputStream );
        pPeerInfo.setID( new Integer( ++zPeerCnt ).toString() );
        zListofPeers.add(
            new ActivePeer( zMyInfo , zUserDialog , this , zPendingPeerManager ,
                            pPeerInfo , pInputStream , pOutputStream ) );
    }
}